#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "star_types.h"
#include "reader.h"
#include "process.h"

// default files
char starship_file[127] = "./data/Starships.op8";
char star_system_file[127] = "./data/Stars.op8";

int main(int argc, char *argv[])
{
	if (argc == 3)
	{
		// run with custom file
		printf("Read the contents of a files provided asarguments.\n");
		strcpy(starship_file, argv[1]);
		strcpy(star_system_file, argv[2]);
	}
	else if (argc == 1)
	{
		// use default file
		printf("Read the default files.\n");
	}
	else
	{
		printf("Usage:\n main [starship file] [star system file]\n\n\n");
		exit(0);
	}

	if (access(starship_file, R_OK) != 0)
	{
		printf("File Error: starship file(%s) doesn't exist.\n", starship_file);
		exit(0);
	}
	if (access(star_system_file, R_OK) != 0)
	{
		printf("File Error: star system file(%s) doesn't exist.\n", star_system_file);
		exit(0);
	}

	Exploration exploration = read_explortaion_file(starship_file, star_system_file);
	process(&exploration);

	free_StarShipList(exploration.starshipList);
	free_StarSystemListNode(exploration.starSystemList);

	return 0;
}